﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZF.WS.UserControls;
using GCELibrary.Current;
using System.Transactions;
using ZF.BLL;
using Newtonsoft.Json;
using ZF.BLL.Admin;
namespace ZF.WS.Admin
{
    public class SiteBlog : ZF.BLL.Page.MMPage
    {
        public override ModuleType Module
        {
            get { return ModuleType.Blog; }
            protected set
            {
                base.Module = value;
            }
        }
        public virtual string ListPage { get { return string.Format("{0}/site/SiteBloglist.aspx", AppKey.SiteKey.ManageDirectory); } }
        public virtual string ManagePage { get { return string.Format("{0}/site/SiteBlogManage.aspx", AppKey.SiteKey.ManageDirectory); } }
        public virtual string CTitle { get { return "名人博客"; } }
    }
    public partial class SiteBlogManage : SiteBlog
    {
    

        [PageArgument(Key = "id", CanNull = false, BindOperType = PageOperType.Edit)]
        public int SiteBlogId { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
    
            if (!IsPostBack)
            {
                if (this.OperType == PageOperType.AddNew)
                    Add();
                else if (this.OperType == PageOperType.Edit)
                    Edit();
            }
        }
        public void InitControls()
        {

            //this.ddlParams.DataSource = DictionaryConfig.MTypeJsonList.Select(p => new
            //{
            //    Key = p.MType.MerchandiseTypeId,
            //    Text = p.MType.Name,
            //});
            //this.ddlParams.DataBind();
            //this.lvMTypeParams.DataSource = DictionaryConfig.MTypeJsonList;
            //this.lvMTypeParams.DataBind();
        }

        public void Add()
        {
            InitControls();

        }
        public void AddSubmit(object sender, EventArgs e)
        {


            using (var dc = SiteInfo.CreateDBDataContext())
            {


                var blog = new ZF.Model.Sys_SiteBlog()
                {
                    Name = this.tbName.Text.Trim(),

                    Url = this.tbUrl.Text,
                    OrderId = this.tbOrderId.Text.Convert<int>(),
                    AddTime = DateTime.Now,
                    Color = null,
                };



                try
                {
                    dc.Sys_SiteBlog.InsertOnSubmit(blog);
                    dc.SubmitChanges();
             
                }
                catch (Exception ex)
                {
                    throw WarnFactory.CreateWarn(ex);
                }
                string msgKey = CTitle;

                SiteInfo.InsertLog("添加" + msgKey, null);
                WarnMessageLink link = new WarnMessageLink(string.Format("成功添加一个{0}，点击确定按钮返回上一页", msgKey));
                link.AddWarnMessageLink(string.Format("继续添加{0}", msgKey), Request.Url.ToString());
                WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success, ListPage).Alert();

            }

        }

        public void Edit()
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {

                var blog = dc.Sys_SiteBlog.FirstOrDefault(p => p.SiteBlogId == this.SiteBlogId);
                if (blog == null)
                    throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
                InitControls();
                this.tbName.Text = blog.Name;
                this.tbOrderId.Text = blog.OrderId.ToString();
                this.tbUrl.Text = blog.Url;

            }
        }
        public void EditSubmit(object sender, EventArgs e)
        {

            using (var ts = new System.Transactions.TransactionScope())
            {
                using (var dc = BLL.SiteInfo.CreateDBDataContext())
                {
                    var blog = dc.Sys_SiteBlog.FirstOrDefault(p => p.SiteBlogId == this.SiteBlogId);
                    if (blog == null)
                        throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
                    blog.Name = this.tbName.Text.Trim();
                    blog.OrderId = this.tbOrderId.Text.Convert<int>();
                    blog.Url = this.tbUrl.Text;

                    try
                    {
                        dc.SubmitChanges();
                        ts.Complete();
                    }
                    catch (Exception ex)
                    {
                        throw WarnFactory.CreateWarn(ex);
                    }
                    string msgKey = CTitle;

                    SiteInfo.InsertLog("修改" + msgKey, null);
                    WarnMessageLink link = new WarnMessageLink(string.Format("成功添加一个{0}，点击确定按钮返回{0}列表", msgKey));

                    link.AddWarnMessageLink(string.Format("重新修改{0}", msgKey), Request.Url.ToString());
                    WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success, ListPage).Alert();

                }


            }

        }

    
    }
 
}
